package com.hunt.service.impl;

import com.github.pagehelper.PageHelper;
import com.hunt.dao.SysPermissionGroupMapper;
import com.hunt.dao.SysPermissionMapper;
import com.hunt.dao.SysRolePermissionMapper;
import com.hunt.model.dto.PageInfo;
import com.hunt.model.dto.SysPermissionDto;
import com.hunt.model.entity.SysPermission;
import com.hunt.model.entity.SysPermissionGroup;
import com.hunt.service.SysPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ouyangan
 * @Date : 2016/10/15
 */
@Transactional
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    
    private static Logger log = LoggerFactory.getLogger(SysPermissionServiceImpl.class);
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysPermissionGroupMapper sysPermissionGroupMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;


    @Override
    public boolean isExistNameExcludeId(long id, long groupId, String name) {
        return sysPermissionMapper.isExistNameExcludeId(id, groupId, name);
    }

    @Override
    public boolean isExistCodeExcludeId(long id, long groupId, String code) {
        return sysPermissionMapper.isExistCodeExcludeId(id, groupId, code);
    }

    /**
     * 判断权限名是否存在
     * @param groupId
     * @param name
     * @return
     */
    @Override
    public boolean isExistName(long groupId, String name) {
        return sysPermissionMapper.isExistName(groupId, name);
    }

    /**
     * 判断权限码是否存在
     * @param groupId
     * @param code
     * @return
     */
    @Override
    public boolean isExistCode(long groupId, String code) {
        return sysPermissionMapper.isExistCode(groupId, code);
    }

    /**
     * 根据id查询权限是否存在
     * @param id
     * @return
     */
    @Override
    public SysPermission selectById(long id) {
        return sysPermissionMapper.selectById(id);
    }

    /**
     * 添加新权限
     * @param sysPermission
     * @return
     */
    @Override
    public long insertPermission(SysPermission sysPermission) {
        Long id = sysPermissionMapper.insert(sysPermission);
        return id;
    }

    /**
     * 删除权限时斩断与角色关系
     * @param id
     */
    @Override
    public void deleteRolePermByPermissionId(long id) {
        sysRolePermissionMapper.deleteRolePermByPermissionId(id);
    }

    /**
     * 更新权限时暂时不斩断与角色关系
     * @param sysPermission
     */
    @Override
    public void update(SysPermission sysPermission) {
        //sysRolePermissionMapper.deleteRolePermByPermissionId(sysPermission.getId());
        sysPermissionMapper.update(sysPermission);
    }

    /**
     * 分页查询列表
     * @param page
     * @param rows
     * @return
     */
    @Override
    public PageInfo selectPage(int page, int rows) {
        int count = sysPermissionMapper.selectCounts();
        PageHelper.startPage(page, rows);
        List<SysPermission> list = sysPermissionMapper.selectAll();
        return new PageInfo(count, list);
    }

    /**
     * 查询列表不分页
     * @return
     */
    @Override
    public PageInfo selectNotPage() {
        List<SysPermission> list = sysPermissionMapper.selectAll();
        return new PageInfo(0, list);
    }

    /**
     * 分页查询权限组
     * @param page
     * @param rows
     * @return
     */
    @Override
    public PageInfo getGroupPermissionList(int page, int rows) {
        int count = sysPermissionMapper.selectCounts();
        PageHelper.startPage(page, rows);
        List<SysPermission> list = sysPermissionMapper.groupPermissionList();
        return new PageInfo(count, list);
    }

    /**
     * 根据id删除权限
     * @param sysPermission
     */
    @Override
    public void delete(SysPermission sysPermission) {
        sysPermissionMapper.delete(sysPermission.getId());
    }
    

    /**
     * 判断权限组名称是否存在
     * @param name
     * @return
     */
    @Override
    public boolean isExistGroupName(String name) {
        return sysPermissionGroupMapper.isExistGroupName(name);
    }

    /**
     * 新增权限组
     * @param sysPermissionGroup
     * @return
     */
    @Override
    public long insertPermissionGroup(SysPermissionGroup sysPermissionGroup) {
        Long id = sysPermissionGroupMapper.insert(sysPermissionGroup);
        return id;
    }

    /**
     * 查询权限组
     * @return
     */
    @Override
    public List<SysPermissionGroup> selectGroup() {
        List<SysPermissionGroup> sysPermissionGroups = sysPermissionGroupMapper.selectAll();
        return sysPermissionGroups;
    }
}
